import mapevent from './common/mapevent.js'
import { LGXEnums } from './common/sysenum.js';

import globalConst from './common/globalConst.js';
let global = globalConst.GlobalConst;

import { KeyGenerator } from './common/keygenerator.js'

import textinfo from './common/struct/basestruct/textinfo.js';

import utilfuncs from './common/tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;

import { CoordTRFUtil } from './common/tools/coordtool.js';
import LGXCOM from './common/common.js'

var EventClassifySvc = {

}

EventClassifySvc.getEventParam = function (dataManger, eventType, resultState, mosEvent, scrnPt, tWorldPt) {
    let canvasScreenRect = dataManger.getCanvasScreenRect();
    let evt = new mapevent.MapEvent;
    evt.eventType = eventType;//绘图画布动作类型
    evt.eventKind = this.getEventKind(dataManger, resultState, mosEvent, tWorldPt);
    var graph = dataManger.getCurentGraph();
    let nearestGraph = null;
    if (mosEvent.type == LGXEnums.LGXMouseMode.LMouseDbClick) {
        nearestGraph = this.getNearestGraph(tWorldPt, graph);
        if (nearestGraph
            && nearestGraph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type
            && nearestGraph.isTitle()) {
            graph = nearestGraph.getContainer();
        }
    }

    if (graph && graph.getStart() == 1 && graph.getFinish() == 0
        && mosEvent.type == LGXEnums.LGXMouseMode.LMouseDown) {
        evt.mouseDownTime = graph.getMouseDownTime();
        evt.drawTool = graph.type;
    }
    if (graph && evt.eventKind == LGXEnums.LGXEventKind.EditTextOnMap) {
        let dstTxtGraph = null
        if (graph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type
            || graph.m_graphType == LGXEnums.LGXGraphType.DimTextGraph_type
            || graph.m_graphType == LGXEnums.LGXGraphType.CADText_type) {
            dstTxtGraph = graph;
        }
        else {
            dstTxtGraph = graph.getLabel();
            if (!dstTxtGraph) {
                let refSymbol = graph.getSymbol();
                if (refSymbol && refSymbol.getMoldActionType() > LGXEnums.moldActionType.common) {
                    dstTxtGraph = graph.getTitleText();
                }
            }
        }
        if (dstTxtGraph) {
            evt.gid = dstTxtGraph.m_gid;
            evt.uuid = dstTxtGraph.m_rid;
        }
    }
    else {
        if (graph) {
            evt.gid = graph.m_gid;
            evt.uuid = graph.m_rid;
        }
    }

    if (evt.eventKind == LGXEnums.LGXEventKind.InSubEditMode) {
        evt.subEditMode = dataManger.getEditMode();
        evt.subEditModeState = dataManger.getEditModeState(evt.subEditMode);
    }

    evt.wx = tWorldPt.x;//系统世界坐标，右手坐标系
    evt.wy = tWorldPt.y;
    evt.scaling = dataManger.getScaling(); //当前缩放比例
    evt.selMultiGraphFlag = dataManger.getMultiSelFlag();//多选标志

    evt.scrnx = mosEvent.clientX;//screenX;//屏幕坐标,相对于显示器屏幕左上角（0，0）
    evt.scrny = mosEvent.clientY;//screenY;

    evt.wndx = scrnPt.x;// mosEvent.offsetx;//窗口坐标,相对于画布所在QWidget的左上角（0，0）
    evt.wndy = scrnPt.y;//mosEvent.offsety;

    evt.controlDown = false;//ctrl键按下
    evt.shiftDown = false;//shift键按下
    evt.altDown = false; //alt键按下
    evt.state = 0; //设备状态值

    evt.leftButtonDown = false;//左键按下
    evt.leftButtonUp = false;//左键弹起
    evt.rightButtonDown = false;//右键按下
    evt.rightButtonUp = false;//右键弹起
    evt.textContent = "";//文本内容
    evt.layerName = ""; //所属逻辑图层名称

    if (graph) {
        evt.xlink = graph.getLink();
        let rect = graph.getZone();
        if (evt.eventKind == LGXEnums.LGXEventKind.EditTextOnMap) {
            if (graph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type) {
                rect = graph.getTextSize();
            }
            else {
                let labelText = graph.getLabel();
                if (labelText) {
                    rect = labelText.getTextSize();
                }
            }
        }
        evt.scope_left_world = rect.left;
        evt.scope_top_world = rect.top;
        evt.scope_right_world = rect.right;
        evt.scope_bottom_world = rect.bottom;
        evt.type = graph.getType();

        let mapInfo = dataManger.getMapViewInfo();
        let leftTopPt = CoordTRFUtil.world2Screen({ x: rect.left, y: rect.top }, mapInfo);
        let rightBotpPt = CoordTRFUtil.world2Screen({ x: rect.right, y: rect.bottom }, mapInfo);
        evt.scope_left_wnd = canvasScreenRect.left + leftTopPt.x;
        evt.scope_top_wnd = canvasScreenRect.top + leftTopPt.y;
        evt.scope_right_wnd = canvasScreenRect.left + rightBotpPt.x;
        evt.scope_bottom_wnd = canvasScreenRect.top + rightBotpPt.y;
        evt.lock = graph.getLock();

        if (graph.m_graphType == LGXEnums.LGXGraphType.umlpackage_type
            && evt.eventKind == LGXEnums.LGXEventKind.DBClickUMLPackge) {
            let packageTitle = graph.getTitleText();
            if (packageTitle && packageTitle.m_graphType == LGXEnums.LGXGraphType.TextGraph_type) {
                evt.textContent = packageTitle.getText();
            }
        }
    }
    else {
        if (evt.eventKind == LGXEnums.LGXEventKind.EditTextOnMap) {
            const defaultWid = 100;
            const defaultHgt = 50;
            evt.scope_left_wnd = canvasScreenRect.left + scrnPt.x - 0.5 * defaultWid;
            evt.scope_top_wnd = canvasScreenRect.top + scrnPt.y - 0.5 * defaultHgt;
            evt.scope_right_wnd = canvasScreenRect.left + scrnPt.x + 0.5 * defaultWid;
            evt.scope_bottom_wnd = canvasScreenRect.top + scrnPt.y + 0.5 * defaultHgt;
        }
    }

    return evt;
}

EventClassifySvc.getEventKind = function (dataManger, resultState, mosEvent, tWorldPt) {
    var evtkind = LGXEnums.LGXEventKind.CommonINFO;
    switch (mosEvent.type) {
        case LGXEnums.LGXMouseMode.LMouseDown:
            evtkind = this.getLMosDownEventKind(dataManger, resultState, mosEvent);
            break;

        case LGXEnums.LGXMouseMode.LMouseUp:
            evtkind = this.getLMosUpEventKind(dataManger, resultState, mosEvent);
            break;

        case LGXEnums.LGXMouseMode.LMouseDbClick:
            evtkind = this.getLMosDBClickEventKind(dataManger, resultState, mosEvent, tWorldPt);
            break;

        case LGXEnums.LGXMouseMode.RMouseUp:
            evtkind = this.getRMosUPEventKind(dataManger, resultState, mosEvent);
            break;
    }
    return evtkind;
}

EventClassifySvc.getRMosUPEventKind = function (dataManger, resultState, mosEvent) {
    var evtkind = LGXEnums.LGXEventKind.CommonINFO;
    let graph = dataManger.getCurentGraph();
    if (graph) {
        evtkind = LGXEnums.LGXEventKind.RMouseUpSelGraph;
    }
    else {
        if (dataManger.getMultiModifyMode()) {
            evtkind = LGXEnums.LGXEventKind.RMouseUpSelGraph;
        }
        else {
            evtkind = LGXEnums.LGXEventKind.RMouseUp;
        }
    }
    return evtkind;
}

EventClassifySvc.getLMosDBClickEventKind = function (dataManger, resultState, mosEvent, tWorldPt) {
    var evtkind = LGXEnums.LGXEventKind.CommonINFO;
    let mapViewInfo = dataManger.getMapViewInfo();
    if (mapViewInfo.editMode == "autocad") {
        evtkind = this.getLMosDBClickEventKind_autocad(dataManger, resultState, mosEvent, tWorldPt);
    }
    else {
        evtkind = this.getLMosDBClickEventKind_common(dataManger, resultState, mosEvent, tWorldPt);
    }

    return evtkind;
}

EventClassifySvc.getLMosDBClickEventKind_autocad = function (dataManger, resultState, mosEvent, tWorldPt) {
    var evtkind = LGXEnums.LGXEventKind.CommonINFO;
    let graph = dataManger.getCurentGraph();
    if (!graph) {
        return evtkind;
    }
    let labelTxt = null;
    if (graph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type
        || graph.m_graphType == LGXEnums.LGXGraphType.DimTextGraph_type
        || graph.m_graphType == LGXEnums.LGXGraphType.CADText_type) {
        labelTxt = graph;
    }
    else {
        labelTxt = graph.getLabel();
        if (labelTxt) {
            dataManger.setCurentGraph(labelTxt);
        }
    }
    if (labelTxt) {
        graph.setSelect(0);
        evtkind = LGXEnums.LGXEventKind.EditTextOnMap;
    }

    return evtkind;
}

EventClassifySvc.getLMosDBClickEventKind_common = function (dataManger, resultState, mosEvent, tWorldPt) {
    var evtkind = LGXEnums.LGXEventKind.CommonINFO;
    let graph = dataManger.getCurentGraph();
    if (!graph) {
        let umlPackageGraph = dataManger.chkDBClickOnPackageHotZone(tWorldPt);
        if (umlPackageGraph) {
            evtkind = LGXEnums.LGXEventKind.DBClickUMLPackge;
            dataManger.setCurentGraph(umlPackageGraph);
        }
        return evtkind;
    }
    else if (graph.m_graphType == LGXEnums.LGXGraphType.umlpackage_type) {
        if (graph.chkClickOnHotZone(tWorldPt)) {
            evtkind = LGXEnums.LGXEventKind.DBClickUMLPackge;
            return evtkind;
        }
    }
    let labelTxt = null;
    if (graph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type) {
        labelTxt = graph;
    }
    else {
        labelTxt = graph.getLabel();
        if (!labelTxt) {
            let refSymbol = graph.getSymbol();
            if (refSymbol && refSymbol.getMoldActionType() > LGXEnums.moldActionType.common) {
                labelTxt = graph.getTitleText();
            }
            else {
                labelTxt = this.notify2CreateLabel(dataManger, graph);
            }
        }
        else {
            dataManger.setCurentGraph(labelTxt);
        }
    }
    if (labelTxt) {
        graph.setSelect(0);
        evtkind = LGXEnums.LGXEventKind.EditTextOnMap;
    }

    return evtkind;
}

EventClassifySvc.notify2CreateLabel = function (dataManger, parentGraph) {
    let toolKind = global.BaseUnitText;
    toolKind = toolKind << 16;
    let pTextGraph = dataManger.productGraph(toolKind);
    if (pTextGraph) {
        pTextGraph.m_gid = KeyGenerator.gid();
        parentGraph.setLabel(pTextGraph);
        dataManger.setCurentGraph(parentGraph);
    }
    else {
        return null;
    }

    let graphZone = parentGraph.getZone();
    let info = new textinfo.TextInfo();
    info.gid = pTextGraph.m_gid;
    let dlftLabPosDecs = "center";
    if (parentGraph.getSymbol()){
        let symbolDef = dataManger.getSymbolDef(parentGraph.getType());
        if (symbolDef) {
            dlftLabPosDecs = symbolDef.dlftLabPos;
        } 
    }
    this.calLabelCenterPt(dlftLabPosDecs,graphZone,info);
    info.m_nUseful = 1;
    info.m_nNewAdd = 1;
    pTextGraph.setUseful(1);
    pTextGraph.setNewAdd(1);
    dataManger.addCurentNewGraph(pTextGraph);

    var list = new Array();
    list.push(parentGraph);
    dataManger.m_pOperStepService.record(1, list);
    pTextGraph.assignInfo(info, 1);
    pTextGraph.resetLabelSize();
    dataManger.m_pOperStepService.record(0, list);
    return pTextGraph;
}

EventClassifySvc.calLabelCenterPt = function(dlftLabPosDecs, graphZone, txtInfo) {
    let cntPt = graphZone.getCnt();
    let labCntPt = { x: 0, y: 0 };
    switch (dlftLabPosDecs) {
        case "center":
            labCntPt.x = cntPt.x;
            labCntPt.y = cntPt.y;
            break;

        case "bottom_center":
            labCntPt.x = cntPt.x;
            labCntPt.y = graphZone.bottom - global.Label2ParentGap - txtInfo.fontsize;
            txtInfo.alignTypeH = "mid";
            txtInfo.alignTypeV = "top";
            break;

        case "top_center":
            labCntPt.x = cntPt.x;
            labCntPt.y = graphZone.top + global.Label2ParentGap + txtInfo.fontsize;
            txtInfo.alignTypeH = "mid";
            txtInfo.alignTypeV = "bottom";
            break;

        default:
            labCntPt.x = cntPt.x;
            labCntPt.y = cntPt.y;
            txtInfo.alignTypeH = "mid";
            txtInfo.alignTypeV = "mid";
            break;
    }

    txtInfo.x = labCntPt.x;
    txtInfo.y = labCntPt.y;
    txtInfo.pos.x = txtInfo.x;
    txtInfo.pos.y = txtInfo.y;
    return labCntPt;
}

EventClassifySvc.getLMosDownEventKind = function (dataManger, resultState, mosEvent) {
    var evtkind = LGXEnums.LGXEventKind.CommonINFO;
    let workmod = dataManger.getMapWorkMode();
    switch (workmod) {
        case LGXEnums.LGXWorkMode.DrawGraphMode:      //绘制模式。外部设置无效，图形内部会自动切换
            evtkind = this.getLMosDownPlotEventKind(dataManger, resultState, mosEvent);
            break;

        case LGXEnums.LGXWorkMode.ModifyMode:    //普通的修改，选中就可以拖动
            evtkind = this.getLMosDownModifyEventKind(dataManger, resultState, mosEvent);
            break;

        case LGXEnums.LGXWorkMode.MultiModifyMode://框选。在系统处于运行模式时外部设置生效。编辑模式时，由图形自动接管
        case LGXEnums.LGXWorkMode.QueryMode:     //非编辑模式，
            break;
    }
    return evtkind;
}

EventClassifySvc.getLMosUpEventKind = function (dataManger, resultState, mosEvent) {
    var evtkind = LGXEnums.LGXEventKind.CommonINFO;
    let workmod = dataManger.getMapWorkMode();
    switch (workmod) {
        case LGXEnums.LGXWorkMode.DrawGraphMode:      //绘制模式。外部设置无效，图形内部会自动切换
            evtkind = this.getLMosUpPlotEventKind(dataManger, resultState, mosEvent);
            break;

        case LGXEnums.LGXWorkMode.ModifyMode:    //普通的修改，选中就可以拖动
            evtkind = this.getLMosUpModifyEventKind(dataManger, resultState, mosEvent);
            break;

        case LGXEnums.LGXWorkMode.MultiModifyMode://框选。在系统处于运行模式时外部设置生效。编辑模式时，由图形自动接管
        case LGXEnums.LGXWorkMode.QueryMode:     //非编辑模式，
            break;

        case LGXEnums.LGXWorkMode.PickPointMode:
            evtkind = LGXEnums.LGXEventKind.Respond2PickPoint;
            break;
    }
    return evtkind;
}

EventClassifySvc.getLMosDownPlotEventKind = function (dataManger, resultState, mosEvent) {
    let tool = dataManger.getTool();
    var mainType = tool >> 16;

    var evtkind = LGXEnums.LGXEventKind.CommonINFO;
    if (mainType == global.BaseUnitText) {
        evtkind = LGXEnums.LGXEventKind.EditTextOnMap;
    }
    return evtkind;
}

EventClassifySvc.getLMosDownModifyEventKind = function (dataManger, resultState, mosEvent) {
    var evtkind = LGXEnums.LGXEventKind.CommonINFO;
    let mode = dataManger.getEditMode();
    if (mode != LGXEnums.EditMode.normal) {
        evtkind = LGXEnums.LGXEventKind.InSubEditMode;
    }
    return evtkind;
}

EventClassifySvc.getLMosUpPlotEventKind = function (dataManger, resultState, mosEvent) {
    let tool = dataManger.getTool();
    var mainType = tool >> 16;
    var evtkind = LGXEnums.LGXEventKind.CommonINFO;
    if (mainType == global.BaseUnitImage) {
        evtkind = LGXEnums.LGXEventKind.StartDrawImageOnMap;
    }
    return evtkind;
}

EventClassifySvc.getLMosUpModifyEventKind = function (dataManger, resultState, mosEvent) {
    var evtkind = LGXEnums.LGXEventKind.CommonINFO;
    let graph = dataManger.getCurentGraph();
    if (graph) {
        evtkind = LGXEnums.LGXEventKind.LMouseSelGraph;
    }
    return evtkind;
}

EventClassifySvc.getNearestGraph = function (mousePt, preGraph) {
    if (!preGraph) {
        return null;
    }
    let childList = preGraph.getAllChild();
    if (!childList || (childList && childList.length == 1)) {
        return preGraph;
    }
    let destGraph = null;
    let mindis = 0;
    for (let i = 0; i < childList.length; i++) {
        let child = childList[i];
        let rect = child.getZone();
        let dis = funcs.distancePoint(mousePt, rect.getCnt(), 1);
        if (funcs.chkPtInRect(mousePt, rect)) {
            if (!destGraph) {
                destGraph = child;
                mindis = dis;
            }
            else if (mindis > dis) {
                destGraph = child;
                mindis = dis;
            }
        }
    }
    return destGraph;
}

export default { EventClassifySvc }